//
//  XQHomeTabBarController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/18.
//

#import "XQHomeTabBarController.h"
#import "XQHomeContainerController.h"
#import "XQOrderListController.h"
#import "XQUserController.h"

NSString *XQTabSelectedCodeMap[] = {
    [0]   = kTabSelectedCodeHome,
    [1]   = kTabSelectedCodeOrder,
    [2]   = kTabSelectedCodeMine,
};


@interface XQHomeTabBarController ()

@end

@implementation XQHomeTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
   
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_14_5
    if (@available(iOS 15.0, *)) {
        [UITableView appearance].sectionHeaderTopPadding = 0;
        [UITableView appearance].prefetchingEnabled = NO;
    }
#endif
    
    NSMutableArray *vcs = [NSMutableArray array];
    NSMutableArray *tabBarItems = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 3; i++) {
        NSString *code = XQTabSelectedCodeMap[i];
        if ([code isEqualToString:kTabSelectedCodeHome]) {
            [vcs addObject:[XQHomeContainerController new]];
            [tabBarItems addObject:[[XQTabBarItem alloc] initWithTitle:@""
                                                                 image:[UIImage imageNamed:@"tabitem_home"]
                                                         selectedImage:[UIImage imageNamed:@"tabitem_home_p"]]];
            
        } else if ([code isEqualToString:kTabSelectedCodeOrder]) {
            [vcs addObject:[XQOrderListController new]];
            [tabBarItems addObject:[[XQTabBarItem alloc] initWithTitle:@""
                                                                 image:[UIImage imageNamed:@"tabitem_order"]
                                                         selectedImage:[UIImage imageNamed:@"tabitem_order_p"]]];
            
        } else if ([code isEqualToString:kTabSelectedCodeMine]) {
            [vcs addObject:[XQUserController new]];
            [tabBarItems addObject:[[XQTabBarItem alloc] initWithTitle:@""
                                                                 image:[UIImage imageNamed:@"tabitem_mine"]
                                                         selectedImage:[UIImage imageNamed:@"tabitem_mine_p"]]];
            
        }
    }
    
    self.viewControllers = vcs;
    self.t_tabBar.tabBarItems = tabBarItems;
}

- (void)goHomeViewController {
    [self setSelectedCode:kTabSelectedCodeHome];
}

- (void)goOrderListController {
    [self setSelectedCode:kTabSelectedCodeOrder];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
    for (XQTabBarItem *item in self.t_tabBar.tabBarItems) {
        item.selected = NO;
    }
    self.t_tabBar.tabBarItems[selectedIndex].selected = YES;
}

- (void)setSelectedCode:(NSString *)selectedCode {
    NSInteger index = [self indexForSelectedCode:selectedCode];
    self.selectedIndex = index;
}

- (NSInteger)indexForSelectedCode:(NSString *)selectedCode {
    NSInteger index = 0;
    for (NSInteger i = 0; i < self.viewControllers.count; i++) {
        if ([selectedCode isEqualToString:XQTabSelectedCodeMap[i]]) {
            index = i;
            break;
        }
    }
    return index;
}

@end
